const {
  GiftShopModel
} = require('../../../../utils/giftshop-model.js');
var giftShopModel = new GiftShopModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderState: -1, //-1: 全部 0:待付款 1.待发货 2:待收货 3:已完成 4:已取消 5:退款中 6:退款成功
    //0: 全部 1:待付款 2:待收货 3:已完成 4:已取消 
    orderList: [], //订单数据
    isHints: false,
    pageIndex: 1,
    pageSize: 20,
    pageCount: 0,
    loadText: "",
    isPassword: false, //支付弹出层
    shopId: '', //门店名称
    orderCode: '', //订单号
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderState: options.orderType || this.data.orderState
    })
  },

  /**
   * 订单列表
   */
  getOrderList() {
    giftShopModel.userOrderList({
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      orderState: parseInt(this.data.orderState), //订单号
      offset: (this.data.pageIndex - 1) * this.data.pageSize,
      limit: this.data.pageSize
    }).then(data => {
      if (data) {
        let pageCountTotal = data.totalCount % this.data.pageSize == 0 ? (data.totalCount / this.data.pageSize) : (parseInt(data.totalCount / this.data.pageSize) + 1);
        this.setData({
          pageCount: pageCountTotal
        })
        if (data.totalCount > 0) {
          // 将新一页的数据添加到原数据后面
          var orderDataList = this.data.orderList;
          var newOrderData = orderDataList.concat(data.data);
          this.setData({
            isLoadData: false,
            isHints: false,
            orderList: newOrderData
          })
        } else {
          this.setData({
            loadText: "",
            isLoadData: false,
            isHints: true,
            orderList: []
          })
        }
      }
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 选择订单类型
   */
  selectOrderType: function (e) {
    wx.reportAnalytics('gift_order_status');
    let type = e.currentTarget.dataset.type //当前索引
    this.setData({
      orderState: type,
      orderList: [], //订单数据
      isHints: false,
      pageIndex: 1,
      pageCount: 0,
      loadText: ""
    }, () => {
      this.getOrderList();
    })
  },

  /**
   * 滚动到底部/右边时触发
   */
  scrollEnd: function () {
    this.setData({
      pageIndex: this.data.pageIndex + 1,
      isLoadData: true,
      loadText: "加载中..."
    }, function () {
      if (this.data.pageIndex > this.data.pageCount) {
        this.setData({
          isLoadData: true,
          loadText: "已经到底了"
        });
      } else {
        this.getOrderList();
      }
    })
  },

  /**
   * 取消订单
   */
  cancelOrder(e) {
    let that = this;
    let code = e.currentTarget.dataset.code //订单号
    let shopId = e.currentTarget.dataset.id //门店id
    wx.showModal({
      title: '是否取消订单？',
      cancelColor: '#333333', //取消文字的颜色
      confirmColor: '#333333', //确定文字的颜色
      confirmText: '确认',
      success(res) {
        if (res.confirm) {
          giftShopModel.userOrderCancel({
            merchantId: wx.getStorageSync('merchantId'), //商户ID
            shopId: shopId, //门店id
            orderCode: code, //订单号
          }).then(data => {
            wx.reportAnalytics('gift_orderlist_cancelorder', {
              order_code: code,
              shop_id: shopId,
            });
            that.setData({
              orderList: [], //订单数据
              pageIndex: 1,
              isHints: false,
              pageCount: 0,
              loadText: ""
            }, () => {
              that.getOrderList();
            })
          }).catch(err => {
            wx.showToast({
              title: `${err.error}`,
              icon: 'none',
              duration: 2000
            })
          })
        } else if (res.cancel) {}
      }
    })
  },

  /**
   * 去支付
   */
  orderPay(e) {
    wx.reportAnalytics('gift_orderlist_orderpay');
    let code = e.currentTarget.dataset.code //订单号
    let shopId = e.currentTarget.dataset.id //门店id
    this.setData({
      shopId: shopId, //门店id
      orderCode: code, //订单号
      isPassword: true
    })
  },

  /**
   * 取消
   */
  closePay: function () {
    wx.reportAnalytics('gift_orderlist_cancelpay');
    this.setData({
      isPassword: false
    })
  },

  /**
   * 支付成功
   */
  orderPaymentSuccess: function () {
    wx.reportAnalytics('gift_orderlist_paysuccess');
    this.setData({
      isPassword: false,
      orderState: 2,
      orderList: [], //订单数据
      isHints: false,
      pageIndex: 1,
      pageCount: 0,
      loadText: ""
    }, () => {
      this.getOrderList();
    })
  },

  /**
   * 跳转到订单详情页面
   */
  goOrderDetail: function (e) {
    wx.reportAnalytics('gift_orderlist_orderdetail');
    let code = e.currentTarget.dataset.code //订单号
    let shopId = e.currentTarget.dataset.id //门店id
    wx.navigateTo({
      url: '/pages/giftShop/myOrder/orderDetails/index?orderCode=' + code + '&shopId=' + shopId
    })
  },
  /**
   * 跳转到物流信息页面
   */
  goOrderTracking: function (e) {
    wx.reportAnalytics('gift_orderlist_logistics');
    let code = e.currentTarget.dataset.code //订单号
    let shopId = e.currentTarget.dataset.id //门店id
    wx.navigateTo({
      url: '/pages/giftShop/myOrder/orderTracking/index?orderCode=' + code + '&shopId=' + shopId
    })
  },

  /**
   * 跳转到服务单详情页面
   */
  goServiceDetail: function (e) {
    wx.reportAnalytics('gift_orderlist_servicedetail');
    let code = e.currentTarget.dataset.code //服务单号
    let shopName = e.currentTarget.dataset.name
    if (code) {
      // wx.navigateTo({
      //   url: '/pages/giftShop/serviceOrder/serviceDetails/index?orderCode=' + code + '&skuId=' + skuId + '&orderStatus=1'
      // })
      wx.navigateTo({
        url: '/pages/giftShop/serviceOrder/afterSale/index?orderCode=' + code + '&shopName=' + shopName
      })
    }
  },

  /**
   * 换货
   */
  setAfterSale: function (e) {
    wx.reportAnalytics('gift_orderlist_exchangegoods');
    let orderCode = e.currentTarget.dataset.code
    let partnerSign = e.currentTarget.dataset.id
    let skuId = e.currentTarget.dataset.sku
    let quantity = e.currentTarget.dataset.num
    giftShopModel.serviceOrderPreCheck({
      merchantId: wx.getStorageSync('merchantId'), //商户ID
      orderCode: orderCode, //订单号
      partnerSign: partnerSign, //第三方id
      skuId: skuId, //商品sku
      quantity: quantity, //换货数量
    }).then(data => {
      wx.navigateTo({
        url: `/pages/giftShop/serviceOrder/afterType/index?currency=${encodeURIComponent(JSON.stringify(data))}`
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none',
        duration: 2000
      })
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      orderList: [], //订单数据
      isHints: false,
      pageIndex: 1,
      pageSize: 20,
      pageCount: 0,
      loadText: "",
      isPassword: false, //支付弹出层
      shopId: '', //门店名称
      orderCode: '', //订单号
    }, () => {
      this.getOrderList();
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.closePay();
  },
})